<?php

namespace app\admin\model\user;

use think\Model;
use traits\model\SoftDelete;

class CardApi extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'user_card';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [];


    public function getOriginData()
    {
        return $this->origin;
    }

    protected static function init()
    {
        self::beforeUpdate(function ($row) {
            $changedata = $row->getChangedData();
            $origin = $row->getOriginData();
            if (isset($changedata['money']) && (function_exists('bccomp') ? bccomp($changedata['money'], $origin['money'], 2) !== 0 : (float)$changedata['money'] !== (float)$origin['money'])) {
                \app\admin\model\UserCardLog::create(['user_card_id' => $row['id'], 'wallet' => $changedata['money'] - $origin['money'], 'before' => $origin['money'], 'after' => $changedata['money'], 'memo' => ($changedata['money'] - $origin['money']) > 0 ? '订单退款' : '小程序消费扣除余额']);
            }
        });
    }








    public function store()
    {
        return $this->belongsTo('app\admin\model\shopro\Store', 'store_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
